package com.franzliszt.OrderOnline.sunzhengyi.LoginPackage;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.franzliszt.OrderOnline.Activity.FunctionActivity;
import com.franzliszt.OrderOnline.SQLite.Dao;
import com.franzliszt.OrderOnline.tool.Goods;
import com.franzliszt.OrderOnline.tool.GoodsList;
import com.franzliszt.OrderOnline.util.KillProcess;
import com.franzliszt.OrderOnline.util.SP;
import com.franzliszt.hospitalnumberingsystem.R;

import java.util.ArrayList;
import java.util.List;


/**
 *
 */
public class Login extends AppCompatActivity {
    private EditText UserName,PassWord;
    private CheckBox AgreementStatus;
    private Button Login;
    private boolean Status = false;
    private Dao dao;
    private List<GoodsList> goodsArrayList = new ArrayList<>();
    private Toast Tips;
    private TextView ErrorTips,Other;
    private int count = 1;
    private SP sp;
    private KillProcess killProcess;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE );
            getWindow().setStatusBarColor( Color.TRANSPARENT );
        }
        setContentView( R.layout.activity_login );

        InitView();
        Listener();
    }
    /*********初始化界面*************/
    private void InitView() {
        UserName = findViewById( R.id.username );
        PassWord = findViewById( R.id.password );
        AgreementStatus = findViewById( R.id.AgreementStatus );
        Login = findViewById( R.id.login );
        Other = findViewById(R.id.exchangeFunc);
        Other.setText("注册");
        dao = new Dao(Login.this);
        goodsArrayList = dao.QueryAll();
        sp = SP.getInstance();
        if (Tips == null) {
            Tips = new Toast( Login.this );
            View view = LayoutInflater.from( Login.this ).inflate( R.layout.passworderror,null,false );
            ErrorTips = view.findViewById(R.id.ErrorTips);
            Tips.setView( view );
            Tips.setGravity( Gravity.CENTER,0,0 );
        }
        killProcess = KillProcess.getInstance();
        killProcess.addActivity(Login.this);
    }
    private void ShowError(String code){
        ErrorTips.setText(code);
        Tips.show();
    }
    //设置标题栏
//    private void SetTitle() {
//        ActionBar actionBar = getSupportActionBar();
//        if (actionBar != null) {
//            actionBar.setDisplayHomeAsUpEnabled(true);
//            actionBar.setHomeButtonEnabled(true);
//        }
//
//    }

//    @Override
//    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
//            switch (item.getItemId()){
//                case android.R.id.home:
//                    Intent MyIntent = new Intent(Intent.ACTION_MAIN);
//                    MyIntent.addCategory(Intent.CATEGORY_HOME);
//                    startActivity(MyIntent);
//                    finish();
//                    break;
//            }
//            return super.onOptionsItemSelected(item);
//    }
    public void Exit(View view){
        killProcess.finishAll();
    }
    public void Exchange(View view){
        ReturnToActivity(RegisterADDForget.class);
    }
    private void ReturnToActivity(Class Activity){
        startActivity(new Intent(Login.this,Activity));
    }
    private void Listener(){
        OnClick onClick = new OnClick();
        Login.setOnClickListener( onClick );
    }
    /****判断CheckBox是否被选中****/
    private boolean AgreementStatusMethod() {
        count++;
        if (count % 2 == 0) {
            return  true;
        }
        return false;
    }


    /**
     *登录函数
     */

    private void SignIn(){
        String GetUserEdit = UserName.getText().toString().trim();
        String GetPassWordEdit = PassWord.getText().toString().trim();
        boolean status = AgreementStatusMethod();

        if (TextUtils.isEmpty( GetUserEdit )){
            UserName.setError( "账号不能为空" );
            return;
        }
        if (TextUtils.isEmpty( GetPassWordEdit )){
            PassWord.setError( "密码不能为空" );
            return;
        }
        if (GetUserEdit.length() > 12){
            UserName.setError( "账号长度不能超过12位" );
            return;
        }
        if (GetPassWordEdit.length() > 12){
            PassWord.setError( "密码长度不能超过12位" );
            return;
        }
        if (!status){
            ShowError("请先勾选隐私服务");
            return;
        }
        if (goodsArrayList != null){
            for (int i = 0; i <goodsArrayList.size() ; i++) {
                if (GetUserEdit.equals(goodsArrayList.get(i).getUserName())){
                    if (GetPassWordEdit.equals(goodsArrayList.get(i).getPassWord())){
                        ShowError("登录成功");
                        //dao.UpdateStatus(GetUserEdit,1);
                        sp.PutData(Login.this,"UserName_login",GetUserEdit);
                        sp.PutData(Login.this,"PassWord_login",GetPassWordEdit);
                        startActivity(new Intent(Login.this, FunctionActivity.class));
                    }else {
                        ShowError("账号或密码错误");
                    }
                    break;
                }
            }
        }else {
            ShowError(" 尚未注册任何账号");
        }
    }
    /*********内部类点击事件*************/
    class OnClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.login:
                    SignIn();
                    break;
                case R.id.AgreementStatus:
                    AgreementStatusMethod();
                    break;
            }

        }
    }

}
